package com.wx.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wx.domain.Doctor;
import com.wx.domain.Patient;
import com.wx.service.DoctorService;
import com.wx.service.PatientService;
import com.wx.service.impl.DoctorServiceImpl;
import com.wx.service.impl.PatientServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 27250
 */
public class LoginServlet extends javax.servlet.http.HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html; charset=UTF-8");

        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String radio = req.getParameter("radio");

        Map<String, Object> map=new HashMap<>();
        ObjectMapper mapper=new ObjectMapper();
        if ("医生".equals(radio)){
            DoctorService doctorService=new DoctorServiceImpl();
            Doctor doctor=doctorService.findByUsername(username);
            if (doctor!=null){
                System.out.println(doctor);
                if (doctor.getPassword().equals(password)){
                    map.put("msg","OK");
                    map.put("isTrue",true);
                    map.put("radio",radio);
                    map.put("userId",doctor.getDoctorId());
                }else {
                    map.put("msg","密码错误");
                    map.put("isTrue",false);
                    map.put("radio",radio);
                }
            }else {
                map.put("msg","用户不存在");
                map.put("isTrue",false);
                map.put("radio",radio);
            }
        }else {
            PatientService patientService=new PatientServiceImpl();
            Patient patient=patientService.findByUsername(username);
            if (patient!=null){
                if (patient.getPassword().equals(password)){
                    map.put("msg","OK");
                    map.put("isTrue",true);
                    map.put("radio",radio);
                    map.put("userId",patient.getPatientId());
                }else {
                    map.put("msg","密码错误");
                    map.put("isTrue",false);
                    map.put("radio",radio);
                }
            }else {
                map.put("msg","用户不存在");
                map.put("isTrue",false);
                map.put("radio",radio);
            }
        }
        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String lastDate = sdf.format(date);
        map.put("lastTime",lastDate);
        mapper.writeValue(resp.getWriter(),map);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
